package com.mc.moring.main.adapter

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseQuickAdapter
import com.mc.moring.R
import com.mc.moring.bean.MyLoanListBean
import com.mc.moring.main.MyLoanDetailsActivity
import com.mc.moring.util.PointMgr
import com.mc.moring.util.StringUtils
import com.mc.moring.util.TimeUtils

class LoanHistoryAdapter(): BaseQuickAdapter<MyLoanListBean.dataBean, LoanHistoryAdapter.VH>(){

    class VH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tv_customerName: TextView = itemView.findViewById(R.id.tv_customerName)
        val tv_periods: TextView = itemView.findViewById(R.id.tv_periods)
        val tv_applyAmount: TextView = itemView.findViewById(R.id.tv_applyAmount)
        val tv_createTime: TextView = itemView.findViewById(R.id.tv_createTime)
        val tv_status: TextView = itemView.findViewById(R.id.tv_status)
        val ll: LinearLayout = itemView.findViewById(R.id.ll)
    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_my_loan, parent, false)
        return VH(view)
    }

    override fun onBindViewHolder(holder: VH, position: Int, item: MyLoanListBean.dataBean?) {

        item?:return


        holder.tv_customerName.text = item.customerName
        holder.tv_periods.text = item.periods
        holder.tv_applyAmount.text = "$" + StringUtils.formatNumberWithCommas(item.applyAmount)
        holder.tv_createTime.text = TimeUtils.date2FormatDetailDate(item.createTime)

        val status = item.paymentStatus
        when(status) {
            "-1" -> {
                holder.tv_status.setBackgroundResource(R.drawable.btn_circle_line_red_bg)
                holder.tv_status.setTextColor(Color.parseColor("#FF3737"))
                holder.tv_status.setText("Rechazado")
            }
            "-2" -> {
                holder.tv_status.setBackgroundResource(R.drawable.btn_circle_line_red_bg)
                holder.tv_status.setTextColor(Color.parseColor("#FF3737"))
                holder.tv_status.setText("Desembolso fallido repetidamente")
            }
            "0" -> {
                holder.tv_status.setBackgroundResource(R.drawable.btn_circle_line_grey_bg)
                holder.tv_status.setTextColor(Color.parseColor("#DADADA"))
                holder.tv_status.setText("Contrato firmando")
            }
            "1" -> {
                holder.tv_status.setBackgroundResource(R.drawable.btn_circle_line_grey_bg)
                holder.tv_status.setTextColor(Color.parseColor("#DADADA"))
                holder.tv_status.setText("Firma fallída")
            }
            "1001" -> {
                holder.tv_status.setBackgroundResource(R.drawable.btn_circle_line_grey_bg)
                holder.tv_status.setTextColor(Color.parseColor("#DADADA"))
                holder.tv_status.setText("Revisando")
            }
            "1002" -> {
                holder.tv_status.setBackgroundResource(R.drawable.btn_circle_line_810_bg)
                holder.tv_status.setTextColor(Color.parseColor("#FF810A"))
                holder.tv_status.setText("Préstamo aprobado")
            }
            "1004" -> {
                holder.tv_status.setBackgroundResource(R.drawable.btn_circle_line_grey_bg)
                holder.tv_status.setTextColor(Color.parseColor("#DADADA"))
                holder.tv_status.setText("Revisión fallida")
            }
            "2001" -> {
                holder.tv_status.setBackgroundResource(R.drawable.btn_circle_line_810_bg)
                holder.tv_status.setTextColor(Color.parseColor("#FF810A"))
                holder.tv_status.setText("Desembolso procesando")
            }
            "2002" -> {
                holder.tv_status.setBackgroundResource(R.drawable.btn_circle_line_green_bg)
                holder.tv_status.setTextColor(Color.parseColor("#00C24E"))
                holder.tv_status.setText("Desembolso exitoso")
            }
            "2003" -> {
                holder.tv_status.setBackgroundResource(R.drawable.btn_circle_line_810_bg)
                holder.tv_status.setTextColor(Color.parseColor("#FF810A"))
                holder.tv_status.setText("Desembolso fallido")
            }
            "2004" -> {
                holder.tv_status.setBackgroundResource(R.drawable.btn_circle_line_810_bg)
                holder.tv_status.setTextColor(Color.parseColor("#FF810A"))
                holder.tv_status.setText("Desembolso procesando")
            }
            "3001" -> {
                holder.tv_status.setBackgroundResource(R.drawable.btn_circle_line_810_bg)
                holder.tv_status.setTextColor(Color.parseColor("#FF810A"))
                holder.tv_status.setText("Reembolso procesando")
            }
            "3002" -> {
                holder.tv_status.setBackgroundResource(R.drawable.btn_circle_line_red_bg)
                holder.tv_status.setTextColor(Color.parseColor("#FF3737"))
                holder.tv_status.setText("Atrasado")
            }
            "3003" -> {
                holder.tv_status.setBackgroundResource(R.drawable.btn_circle_line_red_bg)
                holder.tv_status.setTextColor(Color.parseColor("#FF3737"))
                holder.tv_status.setText("null")
            }
            "4001" -> {
                holder.tv_status.setBackgroundResource(R.drawable.btn_circle_line_green_bg)
                holder.tv_status.setTextColor(Color.parseColor("#00C24E"))
                holder.tv_status.setText("Liquidado")
            }
            else -> {
                holder.tv_status.setBackgroundResource(R.drawable.btn_circle_line_grey_bg)
                holder.tv_status.setTextColor(Color.parseColor("#DADADA"))
                holder.tv_status.setText("Revisando")
            }
        }
        holder.ll.setOnClickListener {
            PointMgr.sendEvent("cl_person_infoof-6_open")
            Log.e("PointMgr.INSTANCE.sendEvent", "cl_person_infoof-6_open")
            val bundle = Bundle()
            bundle.putSerializable("_bean", item)
            val intent = Intent(context, MyLoanDetailsActivity::class.java)
            intent.putExtras(bundle)
            context.startActivity(intent)
        }
    }


}